package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.ClazzQueryPageDto;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;

    @Override
    public Result queryPage(ClazzQueryPageDto clazzQueryPageDto) {
        //分页
        Page<Clazz> page=new Page<>(clazzQueryPageDto.getPage(),clazzQueryPageDto.getPageSize());
        String name = clazzQueryPageDto.getName();
        LocalDate begin = clazzQueryPageDto.getBegin();
        LocalDate end = clazzQueryPageDto.getEnd();
        LambdaQueryWrapper<Clazz> queryWrapper = Wrappers.lambdaQuery();
               queryWrapper .like(StringUtils.isNotEmpty(name), Clazz::getName, name)
                .between(!ObjectUtils.isEmpty(begin)&&!ObjectUtils.isEmpty(end), Clazz::getBeginDate, begin, end);
        this.page(page,queryWrapper);
        //连表查询班主任
        List<Clazz> clazzs = page.getRecords();
        Set<Integer> masterIds = clazzs.stream().map(Clazz::getMasterId).collect(Collectors.toSet());
        if (!ObjectUtils.isEmpty(masterIds)){
            List<Emp> emps = empService.listByIds(masterIds);
            Map<Integer, String> empsMap = emps.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));
            clazzs.forEach(clazz -> clazz.setMasterName(empsMap.get(clazz.getMasterId())));
        }
        //状态

        for (Clazz clazz : clazzs) {
            if (clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开班");
            }else if (clazz.getEndDate().isBefore(LocalDate.now())){
                clazz.setStatus("已结课");
            }else {
                clazz.setStatus("已开班");
            }
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return Result.success(pageResult);

    }

    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        this.save(clazz);
    }

    @Override
    public void updateClazzById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);

    }
}
